<?php
session_start();
require_once('config.php');
if(@$_SESSION['idUser']){
	if(isset($_POST) and $_SERVER['REQUEST_METHOD']=="POST"){
		$valid_formats = array('image/jpeg','image/gif','image/png','image/bmp');
		$file = $_FILES['photoimg']['tmp_name'];
		$name = $_FILES['photoimg']['name'];
		$type = $_FILES['photoimg']['type'];
		$size = ($_FILES['photoimg']['size'] / 1024);
		
		$newName = $_SESSION['idUser'].rand(0,9999999999);
		$photo_300x200 = url_hinh_photo_300x200.$newName.'.jpg';
		$photo_480x320 = url_hinh_photo_480x320.$newName.'.jpg';
		$photo_full = url_hinh_photo_full.$newName.'.jpg';
		
		if(strlen($name)){
			list($txt, $ext) = $type;
			if(in_array($type,$valid_formats)){
				if($size < 5120){
					/* PHPThumb */
					require_once('library/PHPThumb/ThumbLib.inc.php');
					$thumb = PhpThumbFactory::create($file);
					$thumb->adaptiveResize(480,320);
					$thumb->save($photo_480x320);
					/* anh photo_300x200 */
					require_once('class/SimpleImage.php');
					$image = new SimpleImage();
					$image->load($photo_480x320);
					$image->resizeToWidth(300);
					$image->save($photo_300x200);
					/* anh photo_480x320*/
					$image->load($photo_480x320);
					$image->resizeToWidth(480);
					$image->save($photo_480x320);
					/* anh goc */
					$image->load($file);
					$size_info = getimagesize($file);
					$width = $size_info[0];
					$height = $size_info[1];
					if($width>$height && $width>700) $image->resizeToWidth(700);
					elseif($width>=$height && $width<=700) $image->resizeToWidth($width);
					elseif($width<$height && $height>500) $image->resizeToHeight(500);
					elseif($width<$height && $height<=500) $image->resizeToHeight($height);
					else return false;
					$image->save($photo_full);
					
					/* luu tru database */
					$date_create = date('Y-m-d H:i:s');
					if($_SESSION['create_id_album']=='' || $_SESSION['create_id_album']==NULL){
						$album_se = $_SESSION['id_album'];
						//$title = 'title="Nhấp đúp ảnh &quot;Muốn Xóa&quot;" ';
						$id_anh = 'class="xoaanh '.$newName.'"';
					}
					else{
						$album_se = $_SESSION['create_id_album'];
						//$title = 'title="Nhấp đúp ảnh làm &quot;ảnh đại diện album&quot;" ';
						$id_anh = 'class="anhdaidien '.$newName.'"';
					}
					mysql_query("INSERT INTO photos VALUES (NULL,'{$newName}','{$date_create}','".$album_se."','".$_SESSION['idUser']."',NULL,NULL)");
					$photos_id = mysql_insert_id();
					mysql_query("INSERT INTO photos_comment VALUES (NULL,'{$photos_id}','','".$_SESSION['idUser']."','{$date_create}')");
					if($album_se==$_SESSION['album_anh_daidien']){
						$user130x130 = url_hinh_user_130x130.$_SESSION['avatar_user'].'.jpg';
						$user40x40 = url_hinh_user_40x40.$_SESSION['avatar_user'].'.jpg';
						if(file_exists($user130x130) && $_SESSION['avatar_user']!='111111111') unlink($user130x130);
						if(file_exists($user40x40) && $_SESSION['avatar_user']!='111111111') unlink($user40x40);
						
						$thumb->adaptiveResize(130,130);
						$thumb->save(url_hinh_user_130x130.$newName.'.jpg');
						
						$thumb->adaptiveResize(40,40);
						$thumb->save(url_hinh_user_40x40.$newName.'.jpg');
						
						mysql_query("UPDATE users SET avatar='{$newName}' WHERE id='".$_SESSION['idUser']."'");
						$_SESSION['avatar_user'] = $newName;
					}
					
					echo '<img src="'.$photo_300x200.'" '.$title.$id_anh.' /> <script>$(document).ready(function(){ var id=$(".photo_name").val(); $(".photo_name").val(id+"'.$newName.',") ;$("#pic_box_load").html(""); }); </script>';
				}else echo '<font color="#FF0000">Ảnh vượt quá 5MB</font> <script>$(document).ready(function(){ $("#pic_box_load").html(""); }); </script>';					
			} else echo '<font color="#FF0000">Không hỗ trợ loại tập tin này.</font> <script>$(document).ready(function(){ $("#pic_box_load").html(""); }); </script>';	
		}	
		exit;
	}//end upload anh
}